package manager.ikaraoke.database;


import java.util.ArrayList;
import java.util.List;
import manager.ikaraoke.database.dto.FoodDrinkDto;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper{
	
	public DatabaseHelper(Context context) {
		super(context, Constants.dbName, null, Constants.database_version);
		// TODO Auto-generated constructor stub
	}
	@Override
	public void onCreate(SQLiteDatabase sqLiteDatabase) {
		// TODO Auto-generated method stub
		try{
			sqLiteDatabase.execSQL(Constants.dbFoodDrink);
		}catch(SQLException e){
			e.printStackTrace();
		}
		
	}
	//Insert database
	public void insertFoodDrink(FoodDrinkDto foodDrinkDto){
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		try {
			cv.put(Constants.fieldName, foodDrinkDto.getName());
			cv.put(Constants.fieldCost, foodDrinkDto.getCost());
			cv.put(Constants.fieldType, foodDrinkDto.getType());
			db.insert(Constants.dbFoodDrink, null, cv);
		} catch (SQLException e) {
			db.close();
		} finally {
			db.close();
		}		
	}
	//Update database
	public void updateFoodDrink(FoodDrinkDto foodDrinkDto){
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		try {
			cv.put(Constants.fieldName, foodDrinkDto.getName());
			cv.put(Constants.fieldCost, foodDrinkDto.getCost());
			cv.put(Constants.fieldType, foodDrinkDto.getType());
			db.update(Constants.dbFoodDrink, cv, Constants.fieldId + "="
					+ foodDrinkDto.getId(), null);
		} catch (SQLException e) {
			db.close();
		} finally {
			db.close();
		}
		
	}
	//Delete database
	public void deleteFoodDrink(FoodDrinkDto foodDrinkDto){
		SQLiteDatabase db = this.getWritableDatabase();
		try{
			db.delete(Constants.dbFoodDrink, Constants.fieldId + " = ?",new String[] { String.valueOf(foodDrinkDto.getId()) });
		}catch(SQLException e){
			db.close();
		}finally{
			db.close();
		}
	}
	//Get all FoodDrink
	private FoodDrinkDto getCursor(Cursor cursor) {
		FoodDrinkDto foodDrinkDto = new FoodDrinkDto();
		foodDrinkDto.setId(cursor.getInt(0));
		foodDrinkDto.setName(cursor.getString(1));
		foodDrinkDto.setCost(cursor.getFloat(2));
		foodDrinkDto.setType(cursor.getInt(3));
		return foodDrinkDto;
	}
	public List<FoodDrinkDto> getAllFoodDrink() {
		List<FoodDrinkDto> dtos = new ArrayList<FoodDrinkDto>();
		SQLiteDatabase database = this.getReadableDatabase();
		String query = "SELECT  * FROM " + Constants.dbFoodDrink;
		Cursor cursor = database.rawQuery(query, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			FoodDrinkDto foodDrinkDto = getCursor(cursor);
			dtos.add(foodDrinkDto);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return dtos;
	}
	//findbyId
	FoodDrinkDto getFoodDrinkDto(int id) {
		SQLiteDatabase db = this.getReadableDatabase();

		Cursor cursor = db.query(Constants.dbCreateFood, new String[] {
				Constants.fieldId, Constants.fieldName, Constants.fieldCost,
				Constants.fieldType }, Constants.fieldId + "=?",
				new String[] { String.valueOf(id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		FoodDrinkDto foodDrinkDto = new FoodDrinkDto();
		foodDrinkDto = getCursor(cursor);
		return foodDrinkDto;
	}
	@Override
	public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		Log.w(DatabaseHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + Constants.dbName);
		onCreate(sqLiteDatabase);
		
	}

}
